package com.itheima.huikebackend.controller;

import com.itheima.huikebackend.common.PageResult;
import com.itheima.huikebackend.common.R;
import com.itheima.huikebackend.common.Result;
import com.itheima.huikebackend.context.BaseContext;
import com.itheima.huikebackend.dto.SysPostDTO;
import com.itheima.huikebackend.dto.SysPostPageDTO;
import com.itheima.huikebackend.entity.SysPost;
import com.itheima.huikebackend.service.SysPostService;
import com.itheima.huikebackend.vo.SysPostVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "岗位相关接口")
@RequiredArgsConstructor
@RestController
public class SysPostController {
    private final SysPostService sysPostService;

    /**
     * 岗位分页查询
     *
     * @param sysPostPageDTO sys帖子页面dto
     * @return {@link Result}<{@link PageResult}>
     */
    @GetMapping("/system/post/list")
    @ApiOperation("岗位分页查询")
    public R<Map<String, Object>> page(SysPostPageDTO sysPostPageDTO) {
        log.info("岗位分页查询:{}", sysPostPageDTO);
        Map<String, Object> map = sysPostService.page(sysPostPageDTO);
        return R.success(map);
    }

    /**
     * 根据id查询岗位
     *
     * @param id id
     * @return {@link R}<{@link SysPostVO}>
     */
    @GetMapping("/system/post/{id}")
    @ApiOperation("根据id查询岗位")
    public R<SysPostVO> getById(@PathVariable Long id) {
        SysPostVO sysPostVO = sysPostService.getById(id);
        return R.success(sysPostVO);
    }

    /**
     * 更新
     *
     * @param sysPostDTO sys dto后
     * @return {@link R}<{@link ?}>
     */
    @PutMapping("/system/post")
    @ApiOperation("更新岗位")
    public R<?> update(@RequestBody SysPostDTO sysPostDTO) {
        log.info("更新岗位:{}", sysPostDTO);
        sysPostService.update(sysPostDTO);
        return R.success();
    }

    /**
     * 添加
     *
     * @param sysPostDTO sys dto后
     * @return {@link R}
     */
    @PostMapping("/system/post")
    @ApiOperation("新增岗位")
    public R add(@RequestBody SysPostDTO sysPostDTO) {
        log.info("新增岗位:{}", sysPostDTO);


        sysPostService.add(sysPostDTO);
        return R.success();
    }

    /**
     * 删除通过id
     *
     * @param id id
     * @return {@link R}
     */
//    @DeleteMapping("/system/post/{id}")
//    @ApiOperation("删除岗位")
//    public R deleteById(@PathVariable Long id) {
//        log.info("删除岗位:{}", id);
//        sysPostService.deleteById(id);
//        return R.success();
//    }

    @DeleteMapping("/system/post/{ids}")
    @ApiOperation("批量删除岗位")
    public R deleteBatch( @PathVariable List<Long> ids) {
        sysPostService.deleteBatch(ids);
        return R.success();
    }


    /**
     * 导出运营数据报表
     */
    @GetMapping("/system/post/export")
    @ApiOperation("导出运营数据报表")
    public void export(HttpServletResponse response) {
        sysPostService.export(response);
    }
}